package com.zhiyou.app.shop.website.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.stereotype.Component;

import com.zhiyou.app.shop.manager.dao.impl.BaseHibernateDAO;
import com.zhiyou.app.shop.website.dao.IUserStockDao;
import com.zhiyou.app.shop.website.dao.pojo.UserStock;

@Component
public class UserStockDao extends BaseHibernateDAO<UserStock, Integer> implements
		IUserStockDao {

	public UserStockDao(){
		super(UserStock.class);
	}
	
	public List<UserStock> findByStockType(String type) {
		return findByProperty("type", type);
	}

	public int execute(final String sql, final Map<String,Object> params) {
		Integer result = hibernateTemplate.execute(new HibernateCallback<Integer>() {

			public Integer doInHibernate(Session session) throws HibernateException{
				SQLQuery query = session.createSQLQuery(sql);
				if(params != null){
					query.setProperties(params);
				}
				return query.executeUpdate();
			}
			
		});
		return result;
	}

	public int executeHql(final String hql,final Map<String,Object> params){
		Integer result = hibernateTemplate.execute(new HibernateCallback<Integer>() {

			public Integer doInHibernate(Session session) throws HibernateException{
				Query query = session.createQuery(hql);
				if(params != null){
					query.setProperties(params);
				}
				return query.executeUpdate();
			}
			
		});
		return result;
	}

	public long findByPrice(float price,String stockType) {
		final Map<String,Object> params = new HashMap<String,Object>();
		params.put("price", String.valueOf(price));
		params.put("stockType", stockType);
		Long result = hibernateTemplate.execute(new HibernateCallback<Long>() {
			public Long doInHibernate(Session session) throws HibernateException{
				String hql = "select sum(us.stockNum) from UserStock us where us.price = :price "
						+ " and us.stockType = :stockType ";
				Query query = session.createQuery(hql);
				if(params != null){
					query.setProperties(params);
				}
				return ((Number)query.iterate().next()).longValue();
			}
		});
		return result;
	}
	
	public List<UserStock> findByPriceAndType(float price,String stockType,int round,final int page ,final int pageSize){
		final String hql = " from UserStock us where us.price = :price and us.stockType = :stockType and us.round = :round and us.stockNum > 0 order by us.id ";
		final Map<String,Object> params = new HashMap<String,Object>();
		params.put("price", String.valueOf(price));
		params.put("stockType", stockType);
		params.put("round", round-1);
		List<UserStock> result = hibernateTemplate.execute(new HibernateCallback<List<UserStock>>() {
			@SuppressWarnings("unchecked")
			public List<UserStock> doInHibernate(Session session) throws HibernateException{
				Query query = session.createQuery(hql);
				if(params != null){
					query.setProperties(params);
				}
				query.setFirstResult((page-1)*pageSize);
				query.setMaxResults(pageSize);
				return query.list();
			}
		});
		return result;
	}

	/**
	 * @param type : A B C D E F 股
	 */
	public Long findStock(String account, String type) {
		final Map<String, Object> params = new HashMap<String, Object>();
		params.put("account", account);
		params.put("stockType", type);
		Long result = hibernateTemplate.execute(new HibernateCallback<Long>() {
			public Long doInHibernate(Session session) throws HibernateException {
				String hql = " select sum(us.stockNum) from UserStock us where us.userId = :account and us.stockType = :stockType";
				Query query = session.createQuery(hql);
				if(params != null){
					query.setProperties(params);
				}
				try {
					if(query.iterate().next() == null){
						return 0l;
					}else{
						return ((Number)query.iterate().next()).longValue();						
					}
				} catch (Exception e) {
					e.printStackTrace();
					return 0l;
				}
			}
			
		});
 		return result;
	}
	
	public List<UserStock> findByType(String type){
		final String hql = " from UserStock us where us.stockType = :stockType and us.stockNum > 0 order by us.id ";
		final Map<String,Object> params = new HashMap<String,Object>();
		params.put("stockType", type);
		List<UserStock> result = hibernateTemplate.execute(new HibernateCallback<List<UserStock>>() {
			@SuppressWarnings("unchecked")
			public List<UserStock> doInHibernate(Session session) throws HibernateException{
				Query query = session.createQuery(hql);
				if(params != null){
					query.setProperties(params);
				}
				return query.list();
			}
		});
		return result;
	}
	
	public List<UserStock> findByUserId(String userid){
        final String hql = " from UserStock us where us.userId = :userId order by us.id ";
        final Map<String,Object> params = new HashMap<String,Object>();
        params.put("userId", userid);
        List<UserStock> result = hibernateTemplate.execute(new HibernateCallback<List<UserStock>>() {
            @SuppressWarnings("unchecked")
            public List<UserStock> doInHibernate(Session session) throws HibernateException{
                Query query = session.createQuery(hql);
                if(params != null){
                    query.setProperties(params);
                }
                return query.list();
            }
        });
        return result;
    }

}
